package com.ews.onlineexchange.controller;

import java.util.ArrayList;
import java.util.List;

import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.persistence.EntityGraph;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.model.ServiceBuyer;
import com.ews.onlineexchange.model.Trade;
import com.ews.onlineexchange.model.TradeStatus;
import com.ews.onlineexchange.model.User;
import com.ews.onlineexchange.service.EntityQuery;
import com.ews.onlineexchange.service.UserService;

@Model
public class CanceledBulletinList extends EntityQuery<Bulletin> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public CanceledBulletinList() {
		setMaxResults(MAX_RESULTS_NORMAL);
		setOrder("createDate desc");
	}
	@Override
	protected List<Predicate> getRestrictions(CriteriaBuilder cb, Root<Bulletin> entity) {
		List<Predicate> restrictions = new ArrayList<>();
		Join<Bulletin, ServiceBuyer> buyer = entity.join("buyer");
		//根据县区过滤
		super.addBulletinRegionRestriction_view(restrictions,buyer,cb);
		if(StringUtils.isNotBlank(getRegion())){
			restrictions.add(cb.equal(buyer.get("region"), getRegion()));
		}
		restrictions.add(cb.equal(entity.get("delFlag"), DataEntity.DEL_FLAG_NORMAL));
		restrictions.add(cb.isTrue(entity.<Boolean>get("over")));	
		restrictions.add(cb.equal(entity.get("tradeStatus"), TradeStatus.Agreed));
		restrictions.add(cb.isTrue(entity.<Boolean>get("cancelled")));
		String projectName = (String)getParameterValues().get("projectName");
		if (!StringUtils.isBlank(projectName))
			restrictions.add(cb.like(entity.get("projectName"), getStringRestriction(projectName)));
		return restrictions;
	}

	@Override
	protected void setHint( TypedQuery<Bulletin> query ) {
		EntityGraph<?> entityGraph = entityManager.getEntityGraph( "Bulletin.QualificationGroup" );
		query.setHint( "javax.persistence.loadgraph", entityGraph );		
	}
	

}
